import java.util.Scanner;

/**
 * @author ShenTuZhiGang
 * @version 1.0.0
 * @email 1600337300@qq.com
 * @date 2021-09-18 18:03
 */
public class ExamNetEase2021091802 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String[] strings = str.split(" ");
        str = strings[0];
        int n = Integer.parseInt(strings[1]);
        int[] a = new int[str.length() - 1];
        long count = 0;
        long sum = 0;
        long max = 0;
        for (int i = 1; i < str.length(); i++) {
            int i1 = Math.abs(str.charAt(i) - str.charAt(i - 1));
            a[i - 1] = Math.min(26 - i1, i1);
            count += a[i - 1];
            sum += a[i - 1];
            if (i - 1 - n >= 0) {
                count -= a[i - 1 - n];
                max = Math.max(max, count);
            }
        }
        if (a.length > n) {
            System.out.println(Math.min(sum + str.length() - max + n, sum + str.length()));
        } else {
            System.out.println(sum + str.length());
        }

    }
}